package Rbac.view;

import Rbac.domain.dto.UsersDto;
import Rbac.domain.vo.DeptVo;
import Rbac.domain.vo.Result;
import Rbac.domain.vo.RoleVo;
import Rbac.domain.vo.UsersVo;
import Rbac.service.IUsersService;
import Rbac.service.array.UsersService;

public class UsersView extends BaseView<UsersVo, IUsersService>{
    private static final String MENU = """
            --------------------
                   用户系统
            |     0:返回上级     |
            |     1:用户列表     |
            |     2:创建用户     |
            |     3:更新用户     |
            |     4:切换状态     |
            --------------------
            """;
    private static UsersView singleton;
    private final DeptView deptView;
    private final RoleView roleView;

    private UsersView() {
        deptView = DeptView.getInstance();
        roleView = RoleView.getInstance();
        service = UsersService.getInstance();
    }

    public static UsersView getInstance() {
        if (singleton == null) {
            singleton = new UsersView();
        }
        return singleton;
    }

    /**
     * 显示主页面
     */
    public void showMenu() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("已返回上一级菜单！");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> changeStatus();
                default -> System.out.println("输入有误，请输入正确菜单编号！");
            }
        } while (!"0".equals(menuChoice));
    }
    // TODO 用户列表
    public void list() {
        Result result = service.list();
        cache = (UsersVo[]) result.data;
        System.out.println("ID\t用户名\t用户姓名\t手机号码\t邮箱地址\t部门名称\t角色名称\t状态");
        for (UsersVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
                    user.id, user.username, user.name, user.mobile, user.email,
                    user.deptName, user.roleName, user.enabled ? "启用" : "禁用");
        }
    }

    public void create() {
        System.out.print("请输入用户名:");
        String username = MainView.input.nextLine();
        System.out.print("请输入用户姓名:");
        String name = MainView.input.nextLine();
        System.out.print("请输入手机号码:");
        String mobile = MainView.input.nextLine();
        System.out.print("请输入邮箱地址:");
        String email = MainView.input.nextLine();
        System.out.println("请选择部门ID:");
        int deptId = 0;
        do {
            deptView.list();
            if (deptView.cache == null || deptView.cache.length == 0) {
                printError("部门列表为空，请先创建部门！");
                return;
            }
            DeptVo dept = deptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);
        System.out.println("请选择角色ID:");
        int roleId = 0;
        do {
            roleView.list();
            if (roleView.cache == null || roleView.cache.length == 0) {
                printError("角色列表为空，请先创建角色！");
                return;
            }
            RoleVo role = roleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);
        UsersDto dto = new UsersDto(username, name, mobile, email, deptId, roleId);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
        } else {
            printError(result.message);
            list();
        }
    }

    public void update() {
        UsersVo user = getCacheById();
        if (user == null) {
            return;
        }
        System.out.print("请输入用户名，直接回车表示不修改:");
        String username = MainView.input.nextLine();
        username = "".equals(username) ? user.username : username;
        System.out.print("请输入用户姓名，直接回车表示不修改:");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? user.name : name;
        System.out.print("请输入手机号码，直接回车表示不修改:");
        String mobile = MainView.input.nextLine();
        mobile = "".equals(mobile) ? user.mobile : mobile;
        System.out.print("请输入邮箱地址，直接回车表示不修改:");
        String email = MainView.input.nextLine();
        email = "".equals(email) ? user.email : email;
        System.out.println("请选择部门ID，直接回车表示不修改:");
        int deptId = 0;
        do {
            deptView.list();
            if (deptView.cache == null || deptView.cache.length == 0) {
                printError("部门列表为空，请先创建部门！");
                return;
            }
            DeptVo dept = deptView.getCacheById();
            if (dept != null) {
                deptId = dept.id;
            }
        } while (deptId == 0);
        System.out.println("请选择角色ID，直接回车表示不修改:");
        int roleId = 0;
        do {
            roleView.list();
            if (roleView.cache == null || roleView.cache.length == 0) {
                printError("角色列表为空，请先创建角色！");
                return;
            }
            RoleVo role = roleView.getCacheById();
            if (role != null) {
                roleId = role.id;
            }
        } while (roleId == 0);
        UsersDto dto = new UsersDto(username, name, mobile, email, deptId, roleId);
        Result result = service.update(dto, user.id);
        if (!result.success) {
            printError(result.message);
        } else {
            printError(result.message);
            list();
        }
        list();
    }

    public void changeStatus() {
        UsersVo user = getCacheById();
        if (user == null) {
            return;
        }
        if (user.enabled) {
            System.out.println("输入'y'确认停用，输入'n'取消停用:");
        } else {
            System.out.println("输入'y'确认启用，输入'n'取消启用:");
        }
        String confirm = MainView.input.nextLine();
        if (!"y".equalsIgnoreCase(confirm)) {
            System.out.println("您已取消状态修改操作！");
            return;
        }
        Result result = service.changeStatus(user.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }
}

